<?php
include_once ($_SERVER ["DOCUMENT_ROOT"] . "/bolsa_trabajo/util/database.php");
class OfertaCarreraDTO {
	private static $cont = null;
	public function __construct() {
		exit ( 'Init function is not allowed' );
	}
	public static function getIdCarrerasOferta($idOferta) {
		$con = Database::connect ();
		$query = "select carr.id_carrera
				from
				tbl_oferta_carrera as ofecarr,
				tbl_carrera as carr
				where
				ofecarr.tbl_oferta_id_oferta = ? and
				carr.id_carrera = ofecarr.tbl_carrera_id_carrera";
		$stmt = $con->prepare ( $query );
		$stmt->bindParam ( 1, $idOferta );
		$stmt->execute ();
		Database::disconnect ();
		return $stmt;
	}
	public static function removeCarrerasOferta($idOferta) {
		$con = Database::connect ();
		$query = "delete from tbl_oferta_carrera where tbl_oferta_id_oferta = ?";
		$stmt = $con->prepare ( $query );
		$stmt->bindParam ( 1, $idOferta );
		$stmt->execute ();
		Database::disconnect ();
		return $stmt;
	}
	public static function getArrayCarrerasOferta($idOferta) {
		$res = self::getIdCarrerasOferta ( $idOferta );
		$carrerasOferta = array ();
		$num = $res->rowCount ();
		
		if ($num > 0) {
			
			while ( $row = $res->fetch ( PDO::FETCH_ASSOC ) ) {
				// exraer row
				// esto convertira $row['nombres'] a
				// solo $nombres
				extract ( $row );
				
				array_push ( $carrerasOferta, $id_carrera );
			}
			if (! empty ( $carrerasOferta )) {
				$numElemento = count ( $carrerasOferta );
				error_log ( ' El array para la oferta: ' . $idOferta . ' y tiene num de elementos: ' . $numElemento, 0 );
			} else {
				error_log ( ' Array Vacio ' . $idOferta, 0 );
			}
			return $carrerasOferta;
		} else {
			return null;
		}
	}
	public static function getCarrerasDisponibles() {
		$con = Database::connect ();
		$query = "select * from tbl_carrera";
		$stmt = $con->prepare ( $query );
		$stmt->execute ();
		Database::disconnect ();
		return $stmt;
	}
	public static function addCarrerasToOferta($idOferta, $carreras) {
		$con = Database::connect ();
		$query = "insert into tbl_oferta_carrera
				set tbl_oferta_id_oferta = ?, tbl_carrera_id_carrera = ?";
		
		$resultado = true;
		
		foreach ( $carreras as $idCarrera ) {
			$stmt = $con->prepare ( $query );
			$stmt->bindParam ( 1, $idOferta );
			$stmt->bindParam ( 2, $idCarrera );
			
			if (! $stmt->execute ()) {
				$resultado = false;
				break;
			}
			
			error_log ( 'Ingresando: Carrea: ' . $idCarrera . ' oferta: ' . $idOferta, 0 );
		}
		
		Database::disconnect ();
		return $resultado;
	}
	public static function updateCarrerasToOferta($idOferta, $carreras) {
		$con = Database::connect ();
		$query = "insert into tbl_oferta_carrera
				set tbl_oferta_id_oferta = ?, tbl_carrera_id_carrera = ?";
		
		// Borramos las que ya estaban
		self:: removeCarrerasOferta($idOferta);
		//$carrerasYaSeleccionadas = self::getArrayCarrerasOferta ( $idOferta );
		$resultado = true;
		
		foreach ( $carreras as $idCarrera ) {
			// Validamos si no es de las que estaba previamente registrada
			
					$stmt = $con->prepare ( $query );
					$stmt->bindParam ( 1, $idOferta );
					$stmt->bindParam ( 2, $idCarrera );
					
					if (! $stmt->execute ()) {
						$resultado = false;
						break;
					}
					
					error_log ( 'Ingresando: Carrea: ' . $idCarrera . ' oferta: ' . $idOferta, 0 );
				
		
		}
		
		Database::disconnect ();
		return $resultado;
	}
}
?>